home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1995 February: Tool Chest / Dev.CD Feb 95 / Dev.CD Feb 95.toast / Tool Chest / Development Tools & Languages / Dylan Related / Mindy-1.1 (sources only) / mindy-1.1 / interp / module.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-23  |  26.5 KB  |  1,062 lines  |  [TEXT/ttxt]

  1. /**********************************************************************\
  2. *
  3. *  Copyright (c) 1994  Carnegie Mellon University
  4. *  All rights reserved.
  5. *  
  6. *  Use and copying of this software and preparation of derivative
  7. *  works based on this software are permitted, including commercial
  8. *  use, provided that the following conditions are observed:
  9. *  
  10. *  1. This copyright notice must be retained in full on any copies
  11. *     and on appropriate parts of any derivative works.
  12. *  2. Documentation (paper or online) accompanying any system that
  13. *     incorporates this software, or any part of it, must acknowledge
  14. *     the contribution of the Gwydion Project at Carnegie Mellon
  15. *     University.
  16. *  
  17. *  This software is made available "as is".  Neither the authors nor
  18. *  Carnegie Mellon University make any warranty about the software,
  19. *  its performance, or its conformity to any specification.
  20. *  
  21. *  Bug reports, questions, comments, and suggestions should be sent by
  22. *  E-mail to the Internet address "gwydion-bugs@cs.cmu.edu".
  23. *
  24. ***********************************************************************
  25. *
  26. * $Header: module.c,v 1.13 94/08/21 00:43:25 wlott Exp $
  27. *
  28. * This file implements the module system.
  29. *
  30. \**********************************************************************/
  31.  
  32. #include <stdio.h>
  33. #include <stdarg.h>
  34. #include <string.h>
  35. #ifdef sparc
  36. #include <memory.h>
  37. #endif
  38.  
  39. #include "mindy.h"
  40. #include "gc.h"
  41. #include "sym.h"
  42. #include "list.h"
  43. #include "bool.h"
  44. #include "str.h"
  45. #include "obj.h"
  46. #include "module.h"
  47. #include "class.h"
  48. #include "type.h"
  49. #include "thread.h"
  50. #include "func.h"
  51. #include "def.h"
  52. #include "load.h"
  53. #include "error.h"
  54. #include "print.h"
  55.  
  56. obj_t obj_Unbound = NULL;
  57. static obj_t obj_UnboundClass = NULL;
  58.  
  59. static boolean InitializingVars = TRUE;
  60.  
  61. struct bucket {
  62.     obj_t symbol;
  63.     void *datum;
  64.     struct bucket *next;
  65. };
  66.  
  67. struct table {
  68.     int entries;
  69.     int threshold;
  70.     int length;
  71.     struct bucket **table;
  72. };
  73.  
  74. struct library {
  75.     obj_t name;
  76.     struct defn *defn;
  77.     struct table *modules;
  78.     boolean loading;
  79.     boolean busy;
  80.     boolean completed;
  81.     struct library *next;
  82. };
  83.  
  84. struct entry {
  85.     obj_t name;
  86.     void *datum;
  87.     boolean exported;
  88.     obj_t origin;
  89. };
  90.  
  91. struct module {
  92.     obj_t name;
  93.     struct library *home;
  94.     struct defn *defn;
  95.     struct table *variables;
  96.     boolean busy;
  97.     boolean completed;
  98.     struct module *next;
  99. };
  100.  
  101. struct var {
  102.     boolean created;
  103.     struct var *next;
  104.     struct variable variable;
  105. };
  106.  
  107. static struct table *LibraryTable = NULL;
  108. static struct library *Libraries = NULL;
  109. static struct module *Modules = NULL;
  110. static struct var *Vars = NULL;
  111.  
  112. static struct library *library_Dylan = NULL;
  113. struct module *module_BuiltinStuff = NULL;
  114.  
  115. static struct module *make_module(obj_t name, struct library *home);
  116. static struct var *make_var(obj_t name, struct module *home, enum var_kind k);
  117.  
  118.  
  119. /* Table manipulation stuff. */
  120.  
  121. static struct table *make_table(void)
  122. {
  123.     struct table *table = (struct table *)malloc(sizeof(struct table));
  124.  
  125.     table->entries = 0;
  126.     table->threshold = 96;
  127.     table->length = 64;
  128.     table->table = (struct bucket **)malloc(sizeof(struct bucket *)*64);
  129.     memset(table->table, 0, sizeof(struct bucket *)*64);
  130.  
  131.     return table;
  132. }
  133.  
  134. static void *table_lookup(struct table *table, obj_t symbol)
  135. {
  136.     unsigned hash = sym_hash(symbol);
  137.     int index = hash % table->length;
  138.     struct bucket *bucket;
  139.  
  140.     for (bucket = table->table[index]; bucket != NULL; bucket = bucket->next)
  141.     if (bucket->symbol == symbol)
  142.         return bucket->datum;
  143.     return NULL;
  144. }
  145.  
  146. static void rehash_table(struct table *table)
  147. {
  148.     int length = table->length;
  149.     int new_length = (length < 1024) ? (length << 1) : (length + 1024);
  150.     struct bucket **new_table
  151.     = (struct bucket **)malloc(sizeof(struct bucket *) * new_length);
  152.     int i;
  153.  
  154.     memset(new_table, 0, sizeof(struct bucket *) * new_length);
  155.  
  156.     for (i = 0; i < length; i++) {
  157.     struct bucket *bucket, *next;
  158.  
  159.     for (bucket = table->table[i]; bucket != NULL; bucket = next) {
  160.         int index = sym_hash(bucket->symbol) % new_length;
  161.  
  162.         next = bucket->next;
  163.         bucket->next = new_table[index];
  164.         new_table[index] = bucket;
  165.     }
  166.     }
  167.  
  168.     free(table->table);
  169.  
  170.     table->threshold = (new_length * 3) >> 1;
  171.     table->length = new_length;
  172.     table->table = new_table;
  173. }
  174.  
  175. static void table_add(struct table *table, obj_t symbol, void *datum)
  176. {
  177.     unsigned hash = sym_hash(symbol);
  178.     int index = hash % table->length;
  179.     struct bucket *bucket;
  180.  
  181.     for (bucket = table->table[index]; bucket != NULL; bucket = bucket->next)
  182.     if (bucket->symbol == symbol) {
  183.         bucket->datum = datum;
  184.         return;
  185.     }
  186.  
  187.     bucket = (struct bucket *)malloc(sizeof(struct bucket));
  188.     bucket->symbol = symbol;
  189.     bucket->datum = datum;
  190.     bucket->next = table->table[index];
  191.     table->table[index] = bucket;
  192.  
  193.     table->entries++;
  194.     if (table->entries >= table->threshold)
  195.     rehash_table(table);
  196. }
  197.  
  198. static void *table_remove(struct table *table, obj_t symbol)
  199. {
  200.     unsigned hash = sym_hash(symbol);
  201.     int index = hash % table->length;
  202.     struct bucket *bucket, **prev;
  203.  
  204.     for (prev = &table->table[index];
  205.      (bucket = *prev) != NULL;
  206.      prev = &bucket->next)
  207.     if (bucket->symbol == symbol) {
  208.         void *res = bucket->datum;
  209.         *prev = bucket->next;
  210.         free(bucket);
  211.         table->entries--;
  212.         return res;
  213.     }
  214.  
  215.     return NULL;
  216. }
  217.  
  218.  
  219. /* Utilities */
  220.  
  221. static void make_entry(struct table *table, obj_t name, void *datum,
  222.                boolean exported, char *template, ...)
  223. {
  224.     va_list ap;
  225.     char *ptr;
  226.     int len;
  227.     char *origin;
  228.     struct entry *old_entry = table_lookup(table, name);
  229.     struct entry *entry;
  230.  
  231.     if (old_entry && old_entry->datum == datum)
  232.     return;
  233.  
  234.     entry = (struct entry *)malloc(sizeof(struct entry));
  235.  
  236.     entry->name = name;
  237.     entry->datum = datum;
  238.     entry->exported = exported;
  239.  
  240.     len = strlen(template);
  241.     va_start(ap, template);
  242.     for (ptr = template; *ptr != '\0'; ptr++) {
  243.     if (*ptr == '%') {
  244.         len--;
  245.         switch (*++ptr) {
  246.           case '%':
  247.         break;
  248.           case '=':
  249.         len += strlen(sym_name(va_arg(ap, obj_t)));
  250.         break;
  251.           default:
  252.         lose("Bogus thing in origin template: %%%c", *ptr);
  253.         }
  254.     }
  255.     }
  256.     va_end(ap);
  257.  
  258.     entry->origin = alloc_string(len);
  259.     origin = string_chars(entry->origin);
  260.  
  261.     va_start(ap, template);
  262.     for(ptr = template; *ptr != '\0'; ptr++) {
  263.     if (*ptr == '%') {
  264.         switch (*++ptr) {
  265.           case '%':
  266.         *origin++ = '%';
  267.         break;
  268.           case '=':
  269.         {
  270.             char *name = sym_name(va_arg(ap, obj_t));
  271.             while (*name != '\0')
  272.             *origin++ = *name++;
  273.         }
  274.         break;
  275.         }
  276.     }
  277.     else
  278.         *origin++ = *ptr;
  279.     }
  280.     *origin = '\0';
  281.  
  282.     if (old_entry)
  283.     error("%s clashes with %s", entry->origin, old_entry->origin);
  284.  
  285.     table_add(table, name, entry);
  286. }
  287.  
  288. static obj_t prepend_prefix(obj_t name, struct use *use)
  289. {
  290.     char *prefix;
  291.     char *local_name;
  292.     obj_t res;
  293.  
  294.     if (use->prefix == obj_False)
  295.     return name;
  296.  
  297.     prefix = obj_ptr(struct string *, use->prefix)->chars;
  298.     local_name = (char *)malloc(strlen(prefix) + strlen(sym_name(name)) + 1);
  299.     strcpy(local_name, prefix);
  300.     strcat(local_name, sym_name(name));
  301.     res = symbol(local_name);
  302.     free(local_name);
  303.  
  304.     return res;
  305. }
  306.  
  307. static obj_t prefix_or_rename(obj_t name, struct use *use)
  308. {
  309.     obj_t ptr;
  310.  
  311.     for (ptr = use->rename; ptr != obj_Nil; ptr = TAIL(ptr))
  312.     if (name == HEAD(HEAD(ptr)))
  313.         return TAIL(HEAD(ptr));
  314.     return prepend_prefix(name, use);
  315. }
  316.  
  317. static boolean exported(obj_t name, struct use *use)
  318. {
  319.     return use->export == obj_True || memq(name, use->export);
  320. }
  321.  
  322.  
  323. /* Libraries. */
  324.  
  325. static struct library *make_library(obj_t name)
  326. {
  327.     struct library *library = malloc(sizeof(struct library));
  328.  
  329.     library->name = name;
  330.     library->defn = NULL;
  331.     library->modules = make_table();
  332.     library->completed = FALSE;
  333.     library->loading = FALSE;
  334.     library->busy = FALSE;
  335.     library->next = Libraries;
  336.     Libraries = library;
  337.  
  338.     table_add(LibraryTable, name, library);
  339.  
  340.     return library;
  341. }
  342.  
  343. struct library *find_library(obj_t name, boolean createp)
  344. {
  345.     struct library *library = table_lookup(LibraryTable, name);
  346.  
  347.     if (library == NULL && createp) {
  348.     struct defn *defn = malloc(sizeof(struct defn));
  349.     struct use *use1 = malloc(sizeof(struct use));
  350.     struct use *use2 = malloc(sizeof(struct use));
  351.     obj_t dylan_user = symbol("Dylan-User");
  352.     struct module *module = make_module(dylan_user, NULL);
  353.  
  354.     library = make_library(name);
  355.  
  356.     module->defn = defn;
  357.  
  358.     defn->name = dylan_user;
  359.     defn->use = use1;
  360.     defn->exports = obj_Nil;
  361.     defn->creates = obj_Nil;
  362.  
  363.     use1->name = symbol("Dylan");
  364.     use1->import = obj_True;
  365.     use1->prefix = obj_False;
  366.     use1->exclude = obj_Nil;
  367.     use1->rename = obj_Nil;
  368.     use1->export = obj_Nil;
  369.     use1->next = use2;
  370.  
  371.     use2->name = symbol("Extensions");
  372.     use2->import = obj_True;
  373.     use2->prefix = obj_False;
  374.     use2->exclude = obj_Nil;
  375.     use2->rename = obj_Nil;
  376.     use2->export = obj_Nil;
  377.     use2->next = NULL;
  378.  
  379.     make_entry(library->modules, module->name, module, FALSE,
  380.            "module %= implicitly defined in library %=",
  381.            module->name, library->name);
  382.     }
  383.  
  384.     return library;
  385. }
  386.  
  387. void define_library(struct defn *defn)
  388. {
  389.     struct library *library = find_library(defn->name, TRUE);
  390.  
  391.     if (library->defn)
  392.     error("Library %= multiply defined.\n", defn->name);
  393.  
  394.     library->defn = defn;
  395. }
  396.  
  397. static void complete_library(struct library *library)
  398. {
  399.     obj_t ptr;
  400.     struct defn *defn;
  401.     struct use *use;
  402.  
  403.     defn = library->defn;
  404.     if (defn == NULL) {
  405.     if (library->loading)
  406.         error("needed the definition of library %= before the "
  407.           "define library was found",
  408.           library->name);
  409.     library->loading = TRUE;
  410.     load_library(library->name);
  411.     if (library->defn == NULL)
  412.         error("loaded library %=, but never found the define library.\n",
  413.           library->name);
  414.     library->loading = FALSE;
  415.     return;
  416.     }
  417.  
  418.     if (library->busy)
  419.     error("Library %= circularly defined.\n", library->name);
  420.     library->busy = TRUE;
  421.  
  422.     for (ptr = defn->exports; ptr != obj_Nil; ptr = TAIL(ptr)) {
  423.     obj_t name = HEAD(ptr);
  424.     struct module *module = make_module(name, library);
  425.  
  426.     make_entry(library->modules, name, module, TRUE,
  427.            "module %= defined in library %=",
  428.            name, library->name);
  429.     }
  430.  
  431.     for (use = library->defn->use; use != NULL; use = use->next) {
  432.     obj_t use_name = use->name;
  433.     struct library *used_library = find_library(use_name, TRUE);
  434.     obj_t imports = obj_Nil;
  435.  
  436.     if (!used_library->completed)
  437.         complete_library(used_library);
  438.  
  439.     if (use->import == obj_True) {
  440.         struct table *modules = used_library->modules;
  441.         int i;
  442.         for (i = 0; i < modules->length; i++) {
  443.         struct bucket *bucket;
  444.         for (bucket = modules->table[i];
  445.              bucket != NULL;
  446.              bucket = bucket->next) {
  447.             struct entry *entry = (struct entry *)bucket->datum;
  448.             obj_t name = entry->name;
  449.             if (entry->exported && !memq(name, use->exclude)) {
  450.             obj_t local_name = prefix_or_rename(name, use);
  451.             make_entry(library->modules, local_name, entry->datum,
  452.                    exported(local_name, use),
  453.                    name == local_name
  454.                    ? "module %= imported from library %="
  455.                    :"module %= imported from library %= as %=",
  456.                    name, use_name, local_name);
  457.             imports = pair(local_name, imports);
  458.             }
  459.         }
  460.         }
  461.     }
  462.     else {
  463.         for (ptr = use->import; ptr != obj_Nil; ptr = TAIL(ptr)) {
  464.         obj_t name = HEAD(ptr);
  465.         obj_t local_name = prepend_prefix(name, use);
  466.         struct entry *entry
  467.             = table_lookup(used_library->modules, name);
  468.         if (!entry || !entry->exported)
  469.             error("library %= can't import module %= from library %= "
  470.               "because it isn't exported.\n",
  471.               library->name, name, use_name);
  472.         make_entry(library->modules, local_name, entry->datum,
  473.                exported(local_name, use),
  474.                name == local_name
  475.                 ? "module %= imported from library %="
  476.                 : "module %= imported from library %= as %=",
  477.                name, use_name, local_name);
  478.         imports = pair(local_name, imports);
  479.         }
  480.         for (ptr = use->rename; ptr != obj_Nil; ptr = TAIL(ptr)) {
  481.         obj_t rename = HEAD(ptr);
  482.         obj_t name = HEAD(rename);
  483.         obj_t local_name = TAIL(rename);
  484.         struct entry *entry
  485.             = table_lookup(used_library->modules, name);
  486.         if (!entry || !entry->exported)
  487.             error("library %= can't import module %= from library %="
  488.               "because it isn't exported.\n",
  489.               library->name, name, use_name);
  490.         make_entry(library->modules, local_name, entry->datum,
  491.                exported(local_name, use),
  492.                name == local_name
  493.                 ? "module %= imported from library %="
  494.                 : "module %= imported from library %= as %=",
  495.                name, use_name, local_name);
  496.         imports = pair(local_name, imports);
  497.         }
  498.     }
  499.     if (use->export != obj_True)
  500.         for (ptr = use->export; ptr != obj_Nil; ptr = TAIL(ptr))
  501.         if (!memq(HEAD(ptr), imports))
  502.             error("library %= can't re-export module %= because it "
  503.               "doesn't import it in the first place",
  504.               library->name, HEAD(ptr));
  505.     }
  506.  
  507.     library->busy = FALSE;
  508.     library->completed = TRUE;
  509. }
  510.  
  511.  
  512. /* Modules */
  513.  
  514. static struct module *make_module(obj_t name, struct library *home)
  515. {
  516.     struct module *module = malloc(sizeof(struct module));
  517.  
  518.     module->name = name;
  519.     module->home = home;
  520.     module->defn = NULL;
  521.     module->variables = make_table();
  522.     module->busy = FALSE;
  523.     module->completed = FALSE;
  524.     module->next = Modules;
  525.     Modules = module;
  526.  
  527.     return module;
  528. }
  529.  
  530. struct module *find_module(struct library *library, obj_t name,
  531.                boolean lose_if_not_there, boolean lose_if_imported)
  532. {
  533.     struct entry *entry;
  534.     struct module *module;
  535.  
  536.     if ((entry = table_lookup(library->modules, name)) == NULL) {
  537.     if (!lose_if_not_there)
  538.         return NULL;
  539.  
  540.     if (!library->completed) {
  541.         complete_library(library);
  542.         if ((entry = table_lookup(library->modules, name)) == NULL)
  543.         error("Unknown module %= in library %=",
  544.               name, library->name);
  545.     }
  546.     else
  547.         error("Unknown module %= in library %=", name, library->name);
  548.     }
  549.  
  550.     module = entry->datum;
  551.  
  552.     if (lose_if_imported && module->home != library && module->home != NULL)
  553.     error("Can't add code to module %= in library %= because it "
  554.           "is imported from library %=",
  555.           module->name,
  556.           library->name,
  557.           module->home->name);
  558.  
  559.     return module;
  560. }
  561.  
  562. void define_module(struct library *library, struct defn *defn)
  563. {
  564.     struct entry *entry;
  565.     struct module *module;
  566.  
  567.     entry = table_lookup(library->modules, defn->name);
  568.     if (entry == NULL && !library->completed) {
  569.     complete_library(library);
  570.     entry = table_lookup(library->modules, defn->name);
  571.     }
  572.     if (entry == NULL) {
  573.     module = make_module(defn->name, library);
  574.     make_entry(library->modules, defn->name, module, FALSE,
  575.            "module %= internal to library %=",
  576.            defn->name, library->name);
  577.     }
  578.     else {
  579.     module = entry->datum;
  580.  
  581.     if (module->home != library)
  582.         error("Can't define %s in library %=.\n",
  583.           entry->origin, library->name);
  584.  
  585.     if (module->defn)
  586.         error("Module %= multiply defined.\n", defn->name);
  587.     }
  588.  
  589.     module->defn = defn;
  590. }
  591.  
  592. static void
  593.     make_and_export_var(struct module *module, obj_t name, boolean created)
  594. {
  595.     struct entry *entry;
  596.     struct var *var;
  597.  
  598.     entry = table_lookup(module->variables, name);
  599.     if (entry) {
  600.     var = entry->datum;
  601.     if (var->variable.home == module) {
  602.         table_remove(module->variables, name);
  603.         free(entry);
  604.     }
  605.     else
  606.         var = make_var(name, module, var_Assumed);
  607.     }
  608.     else
  609.     var = make_var(name, module, var_Assumed);
  610.  
  611.     make_entry(module->variables, name, var, TRUE,
  612.            created
  613.            ? "variable %= created in module %="
  614.            : "variable %= defined in module %=",
  615.            name, module->name);
  616.  
  617.     var->created = created;
  618. }
  619.  
  620. static void complete_module(struct module *module)
  621. {
  622.     obj_t ptr;
  623.     struct defn *defn;
  624.     struct use *use;
  625.  
  626.     if (module->busy)
  627.     error("Module %= circularly defined.", module->name);
  628.     module->busy = TRUE;
  629.  
  630.     defn = module->defn;
  631.     if (defn == NULL)
  632.     error("Attempt to use module %= before it is defined.", module->name);
  633.  
  634.     for (ptr = defn->exports; ptr != obj_Nil; ptr = TAIL(ptr))
  635.     make_and_export_var(module, HEAD(ptr), FALSE);
  636.     for (ptr = defn->creates; ptr != obj_Nil; ptr = TAIL(ptr))
  637.     make_and_export_var(module, HEAD(ptr), TRUE);
  638.  
  639.     for (use = module->defn->use; use != NULL; use = use->next) {
  640.     obj_t use_name = use->name;
  641.     struct library *home = module->home ? module->home : library_Dylan;
  642.     struct module *used_module = find_module(home, use_name, TRUE, FALSE);
  643.     obj_t imports = obj_Nil;
  644.  
  645.     if (!used_module->completed)
  646.         complete_module(used_module);
  647.  
  648.     if (use->import == obj_True) {
  649.         struct table *modules = used_module->variables;
  650.         int i;
  651.         for (i = 0; i < modules->length; i++) {
  652.         struct bucket *bucket;
  653.         for (bucket = modules->table[i];
  654.              bucket != NULL;
  655.              bucket = bucket->next) {
  656.             struct entry *entry = (struct entry *)bucket->datum;
  657.             obj_t name = entry->name;
  658.             if (entry->exported && !memq(name, use->exclude)) {
  659.             obj_t local_name = prefix_or_rename(name, use);
  660.             make_entry(module->variables, local_name, entry->datum,
  661.                    exported(local_name, use),
  662.                    name == local_name
  663.                    ? "variable %= imported from module %="
  664.                    : "variable %= imported from "
  665.                      "module %= as %=",
  666.                    name, use_name, local_name);
  667.             imports = pair(local_name, imports);
  668.             }
  669.         }
  670.         }
  671.     }
  672.     else {
  673.         for (ptr = use->import; ptr != obj_Nil; ptr = TAIL(ptr)) {
  674.         obj_t name = HEAD(ptr);
  675.         obj_t local_name = prepend_prefix(name, use);
  676.         struct entry *entry
  677.             = table_lookup(used_module->variables, name);
  678.         if (!entry || !entry->exported)
  679.             error("module %= can't import variable %= from module %= "
  680.               "because it isn't exported.",
  681.               module->name, name, use_name);
  682.         make_entry(module->variables, local_name, entry->datum,
  683.                exported(local_name, use),
  684.                name == local_name
  685.                 ? "variable %= imported from module %="
  686.                 : "variable %= imported from module %= as %=",
  687.                name, use_name, local_name);
  688.         imports = pair(local_name, imports);
  689.         }
  690.         for (ptr = use->rename; ptr != obj_Nil; ptr = TAIL(ptr)) {
  691.         obj_t rename = HEAD(ptr);
  692.         obj_t name = HEAD(rename);
  693.         obj_t local_name = TAIL(rename);
  694.         struct entry *entry
  695.             = table_lookup(used_module->variables, name);
  696.         if (!entry || !entry->exported)
  697.             error("module %= can't import variable %= from module %="
  698.               "because it isn't exported.",
  699.               module->name, name, use_name);
  700.         make_entry(module->variables, local_name, entry->datum,
  701.                exported(local_name, use),
  702.                name == local_name
  703.                 ? "variable %= imported from module %="
  704.                 : "variable %= imported from module %= as %=",
  705.                name, use_name, local_name);
  706.         imports = pair(local_name, imports);
  707.         }
  708.     }
  709.     if (use->export != obj_True)
  710.         for (ptr = use->export; ptr != obj_Nil; ptr = TAIL(ptr))
  711.         if (!memq(HEAD(ptr), imports))
  712.             error("module %= can't re-export variable %= because it "
  713.               "doesn't import it in the first place",
  714.               module->name, HEAD(ptr));
  715.     }
  716.  
  717.     module->busy = FALSE;
  718.     module->completed = TRUE;
  719. }
  720.  
  721.  
  722. /* Variables. */
  723.  
  724. static struct var *make_var(obj_t name, struct module *home,enum var_kind kind)
  725. {
  726.     struct var *var = malloc(sizeof(struct var));
  727.  
  728.     var->created = FALSE;
  729.     var->next = Vars;
  730.     Vars = var;
  731.  
  732.     var->variable.name = name;
  733.     var->variable.home = home;
  734.     var->variable.kind = kind;
  735.     var->variable.value = obj_Unbound;
  736.     var->variable.type = obj_False;
  737.     var->variable.function = func_Maybe;
  738.  
  739.     return var;
  740. }
  741.  
  742. struct var *find_var(struct module *module, obj_t name, boolean writeable,
  743.              boolean createp)
  744. {
  745.     struct entry *entry = table_lookup(module->variables, name);
  746.     struct var *var;
  747.  
  748.     if (entry == NULL) {
  749.     if (createp) {
  750.         if (!module->completed && !InitializingVars) {
  751.         complete_module(module);
  752.         entry = table_lookup(module->variables, name);
  753.         }
  754.     }
  755.     else
  756.         return NULL;
  757.     }
  758.     if (entry == NULL) {
  759.     var = make_var(name, module,
  760.                writeable ? var_AssumedWriteable : var_Assumed);
  761.     make_entry(module->variables, name, var, FALSE,
  762.            "variable %= internal to module %=",
  763.            name, module->name);
  764.     }
  765.     else {
  766.     var = entry->datum;
  767.     if (writeable) {
  768.         switch (var->variable.kind) {
  769.           case var_Assumed:
  770.         var->variable.kind = var_AssumedWriteable;
  771.         break;
  772.           case var_AssumedWriteable:
  773.           case var_Variable:
  774.         break;
  775.           default:
  776.         error("attempt to change constant %= from module %=.",
  777.               name, module->name);
  778.         }
  779.     }
  780.     }
  781.     return var;
  782. }
  783.  
  784. struct variable
  785.     *find_variable(struct module *module, obj_t name, boolean writeable,
  786.            boolean createp)
  787. {
  788.     struct var *var = find_var(module, name, writeable, createp);
  789.  
  790.     if (var)
  791.     return &var->variable;
  792.     else
  793.     return NULL;
  794. }
  795.  
  796. void define_variable(struct module *module, obj_t name, enum var_kind kind)
  797. {
  798.     struct var *var = find_var(module, name, kind == var_Variable, TRUE);
  799.  
  800.     switch (var->variable.kind) {
  801.       case var_Assumed:
  802.     if (kind != var_Method)
  803.         var->variable.kind = kind;
  804.     break;
  805.       case var_AssumedWriteable:
  806.     if (kind == var_Variable)
  807.         var->variable.kind = var_Variable;
  808.     else if (kind != var_Method)
  809.         error("attempt to change constant %= from module %=.",
  810.           name, module->name);
  811.     break;
  812.       default:
  813.     if (kind != var_Method)
  814.         error("variable %= in module %= multiply defined.",
  815.           name, module->name);
  816.     break;
  817.     }
  818.  
  819.     if (var->created) {
  820.     if (var->variable.home == module)
  821.         error("variable %= must be defined by a user of module %=\n",
  822.           name, module->name);
  823.     else if (kind!=var_Method && var->variable.home->home!=module->home)
  824.         if (module->home)
  825.         error("variable %= must be defined in library %=, not %=\n",
  826.               name, var->variable.home->home->name,
  827.               module->home->name);
  828.         else
  829.         error("variable %= must be defined in library %=\n",
  830.               name, var->variable.home->home->name);
  831.     }
  832.     else {
  833.     if (kind != var_Method && var->variable.home != module)
  834.         error("variable %= must be defined in module %=, not %=\n",
  835.           name, var->variable.home->name, module->name);
  836.     }
  837. }
  838.  
  839.  
  840. /* Debugger support. */
  841.  
  842. void list_libraries(void)
  843. {
  844.     struct library *library;
  845.  
  846.     for (library = Libraries; library != NULL; library = library->next) {
  847.     prin1(library->name);
  848.     if (library->completed)
  849.         printf("\n");
  850.     else if (library->defn)
  851.         printf(" [defined, but not filled in.]\n");
  852.     else
  853.         printf(" [no definition]\n");
  854.     }
  855. }
  856.  
  857. obj_t library_name(struct library *library)
  858. {
  859.     return library->name;
  860. }
  861.  
  862. void list_modules(struct library *library)
  863. {
  864.     struct table *table = library->modules;
  865.     int i;
  866.     struct bucket *bucket;
  867.     
  868.     for (i = 0; i < table->length; i++) {
  869.     for (bucket = table->table[i]; bucket != NULL; bucket = bucket->next) {
  870.         struct entry *entry = bucket->datum;
  871.         struct module *module = entry->datum;
  872.  
  873.         printf("%c%c ",
  874.            entry->exported ? 'x' : ' ',
  875.            (module->home == NULL || module->home == library)
  876.              ? ' ' : 'i');
  877.         prin1(entry->name);
  878.         if (module->completed)
  879.         printf("\n");
  880.         else if (module->defn)
  881.         printf(" [defined, but not filled in.]\n");
  882.         else
  883.         printf(" [no definition]\n");
  884.     }
  885.     }
  886. }
  887.  
  888. obj_t module_name(struct module *module)
  889. {
  890.     return module->name;
  891. }
  892.  
  893.  
  894.  
  895. /* GC stuff. */
  896.  
  897. static int scav_unbound(struct object *ptr)
  898. {
  899.     return sizeof(struct object);
  900. }
  901.  
  902. static obj_t trans_unbound(obj_t unbound)
  903. {
  904.     return transport(unbound, sizeof(struct object));
  905. }
  906.  
  907. static void scav_use(struct use *use)
  908. {
  909.     scavenge(&use->name);
  910.     scavenge(&use->import);
  911.     scavenge(&use->prefix);
  912.     scavenge(&use->exclude);
  913.     scavenge(&use->rename);
  914.     scavenge(&use->export);
  915. }
  916.  
  917. static void scav_defn(struct defn *defn)
  918. {
  919.     struct use *use;
  920.  
  921.     scavenge(&defn->name);
  922.     scavenge(&defn->exports);
  923.     scavenge(&defn->creates);
  924.  
  925.     for (use = defn->use; use != NULL; use = use->next)
  926.     scav_use(use);
  927. }
  928.  
  929. static void scav_table(struct table *table, boolean of_entries)
  930. {
  931.     int i;
  932.     struct bucket *bucket;
  933.     
  934.     for (i = 0; i < table->length; i++) {
  935.     for (bucket = table->table[i]; bucket != NULL; bucket = bucket->next) {
  936.         scavenge(&bucket->symbol);
  937.         if (of_entries) {
  938.         struct entry *entry = bucket->datum;
  939.         scavenge(&entry->name);
  940.         scavenge(&entry->origin);
  941.         }
  942.     }
  943.     }
  944. }
  945.  
  946. static void scav_var(struct var *var)
  947. {
  948.     scavenge(&var->variable.name);
  949.     scavenge(&var->variable.value);
  950.     scavenge(&var->variable.type);
  951. }
  952.  
  953. static void scav_module(struct module *module)
  954. {
  955.     scavenge(&module->name);
  956.     scav_defn(module->defn);
  957.     scav_table(module->variables, TRUE);
  958. }
  959.  
  960. static void scav_library(struct library *library)
  961. {
  962.     scavenge(&library->name);
  963.     scav_defn(library->defn);
  964.     scav_table(library->modules, TRUE);
  965. }
  966.  
  967. void scavenge_module_roots(void)
  968. {
  969.     struct library *library;
  970.     struct module *module;
  971.     struct var *var;
  972.  
  973.     scav_table(LibraryTable, FALSE);
  974.     for (library = Libraries; library != NULL; library = library->next)
  975.     scav_library(library);
  976.     for (module = Modules; module != NULL; module = module->next)
  977.     scav_module(module);
  978.     for (var = Vars; var != NULL; var = var->next)
  979.     scav_var(var);
  980.     scavenge(&obj_Unbound);
  981.     scavenge(&obj_UnboundClass);
  982. }
  983.  
  984.  
  985. /* Initialization stuff. */
  986.  
  987. void make_module_classes(void)
  988. {
  989.     obj_UnboundClass = make_builtin_class(scav_unbound, trans_unbound);
  990. }
  991.  
  992. void init_modules(void)
  993. {
  994.     obj_t dylan = symbol("Dylan");
  995.     obj_t stuff = symbol("Builtin-Stuff");
  996.  
  997.     LibraryTable = make_table();
  998.  
  999.     library_Dylan = find_library(dylan, TRUE);
  1000.  
  1001.     {
  1002.     /* Define the dylan-user library. */
  1003.     struct defn *defn = malloc(sizeof(*defn));
  1004.     struct use *use = malloc(sizeof(*use));
  1005.     defn->name = symbol("Dylan-User");
  1006.     defn->use = use;
  1007.     defn->exports = obj_Nil;
  1008.     defn->creates = NULL;
  1009.     use->name = dylan;
  1010.     use->import = obj_True;
  1011.     use->prefix = obj_False;
  1012.     use->exclude = obj_Nil;
  1013.     use->rename = obj_Nil;
  1014.     use->export = obj_Nil;
  1015.     use->next = NULL;
  1016.     define_library(defn);
  1017.     }
  1018.     
  1019.     module_BuiltinStuff = make_module(stuff, library_Dylan);
  1020.     make_entry(library_Dylan->modules, stuff, module_BuiltinStuff, FALSE,
  1021.            "module %= internal to library %=", stuff, dylan);
  1022.  
  1023.     obj_Unbound = alloc(obj_UnboundClass, sizeof(struct object));
  1024. }
  1025.  
  1026. void init_module_classes(void)
  1027. {
  1028.     init_builtin_class(obj_UnboundClass, "<unbound-marker>",
  1029.                obj_ObjectClass, NULL);
  1030. }
  1031.  
  1032. void done_initializing_vars(void)
  1033. {
  1034.     InitializingVars = FALSE;
  1035. }
  1036.  
  1037. void finalize_modules(void)
  1038. {
  1039.     struct library *library;
  1040.     struct module *module;
  1041. #if 0
  1042.     struct var *var;
  1043. #endif
  1044.  
  1045.     for (library = Libraries; library != NULL; library = library->next)
  1046.     if (!library->completed)
  1047.         complete_library(library);
  1048.     for (module = Modules; module != NULL; module = module->next)
  1049.     if (!module->completed)
  1050.         complete_module(module);
  1051. #if 0
  1052.     for (var = Vars; var != NULL; var = var->next)
  1053.     if (var->variable.kind == var_Assumed
  1054.         || var->variable.kind == var_AssumedWriteable)
  1055.         fprintf(stderr,
  1056.             "variable %s in module %s in library %s undefined\n",
  1057.             sym_name(var->variable.name),
  1058.             sym_name(var->variable.home->name),
  1059.             sym_name(var->variable.home->home->name));
  1060. #endif
  1061. }
  1062.